/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cageballserver.io;

import cageballcommon.messages.Action;
import cageballcommon.messages.InitTeam;
import cageballcommon.exceptions.CageballIOException;
import cageballcommon.exceptions.CageballParseException;
import cageballcommon.io.interfaces.CageballIO;
import cageballcommon.messages.State;
import cageballserver.CageballClient;
import java.util.Properties;

/**
 *
 * @author kanonas
 */
public class Client implements CageballClient {

    private InitTeam initTeam;
    private int id;
    private CageballIO io;
    private ClientState state;
    private GameServer server;

    public Client(CageballIO io, int id, GameServer server) {

        this.id = id;
        this.io = io;
        this.server = server;

        state = ClientState.CONNECTED;
    }

    public Action readAction() throws CageballIOException, CageballParseException {
        Action action = io.readAction();
        action.setTeam(id);
        return action;
    }

    public void writeState(State state) throws CageballIOException {
        io.writeState(state);
    }

    public void writeInitTeam(InitTeam otherTeam) throws CageballIOException {
        io.writeInitTeam(initTeam);
    }

    public InitTeam getInitTeam() {
        return initTeam;
    }

    public int getId() {
        return id;
    }

    public ClientState getClientState() {
        return state;
    }

    public void setClientState(ClientState state) {
        this.state = state;
    }

    public void close() throws CageballIOException {
        io.close();
        server.removeClient(this);
    }

    public State readState() throws CageballIOException, CageballParseException {
        return io.readState();
    }

    public InitTeam readInitTeam() throws CageballIOException, CageballParseException {
        return io.readInitTeam();
    }

    public Properties readProperties() throws CageballIOException, CageballParseException {
        return io.readProperties();
    }

    public void writeAction(Action action) throws CageballIOException {
        io.writeAction(action);
    }

    public void writeProperties(Properties properties) throws CageballIOException {
        io.writeProperties(properties);
    }

    public void setInitTeam(InitTeam initTeam) {
        this.initTeam = initTeam;
        this.initTeam.setId(id);
    }

    public void setId(int id) {
        this.id = id;
    }

    public String toString() {

        if (initTeam == null) {
            return "Connecting...";
        }
        return initTeam.getTeamName();
    }
}
